import {Order} from '../orderOk/order-model.js';
import {User} from '../user/user-model.js';
var user = new User();
var order = new Order();
var app = getApp();
Page({
  data: {
    closeShow:true
  },
  onLoad(options) {
    this.setData({
      options:options
    })
   this._loadData(options);
  },
  _loadData:function(options)
  {
    order.getOrderRentAndOrderProduct(options.id,(data)=>{
      if(data.code ==1)
      {
        my.hideLoading();
      }
      this.setData({
        rentArr:data.data
      })
    })
  },

  doOrder:function(e)
  {
    
    if(this.data.rentArr.receipt_status!=20)
    {
      my.showToast({
        type:'fail',
        content:"收到货后再还款吧",
      });
      return;
    }
    var data = {
      order_rent_id:e.currentTarget.dataset.id,
      order_id:e.currentTarget.dataset.orderid,
      rent_price:e.currentTarget.dataset.rentprice,
      goods_name:this.data.rentArr.order_product.goods_name,
      goods_attr:this.data.rentArr.order_product.goods_attr,
      rent_number:e.currentTarget.dataset.number,
      uid:app.globalData.uid
      
    };
    order.payBack(data,(data)=>{
        if(data.code==0)
        {
           my.showToast({
                  type:'fail',
                  content:data.msg,
                  success: (res) => {
                      this._loadData(this.data.options);
                    },
                  });
                   return;
        }else{
        var orderId = data.data.order_id;
        var orderNo = data.data.order_no;
        this.payBackOrder(orderId,orderNo,(data)=>{

        })
        }
    })
  },

  payBackOrder:function(orderId,orderNo)
  {
    order.payBackReturn(orderId,orderNo,(data)=>{
         var out_trade_no = data.data.alipay_trade_create_response.out_trade_no;
         var trade_no = data.data.alipay_trade_create_response.trade_no;
         my.tradePay({
           tradeNO:trade_no,
           success: (res) => {
             if(res.resultCode ==9000)
                {
                 order.payBackNotify(orderId,(data)=>{
                    if(data.code ==1 && data.data == true)
                    {
                     my.showToast({
                       type:'none',
                       content:"还款成功",
                       success: (res) => {
                         this._loadData(this.data.options);
                       },
                     });
                    }
                 })
                }else{
                   my.showToast({
                       type:'fail',
                       content:"还款失败",
                       success: (res) => {
                         this._loadData(this.data.options);
                       },
                     });
                }
           },
         });
    })
  },

  rentsend:function(e)
  {
    if(this.data.rentArr.return_status==10)
    {
      my.showToast({
        type:'fail',
        content:"还款未完成不能买断",
        success: (res) => {
          
        },
      });
      return;
    }
    var id = e.currentTarget.dataset.id;
    user.buyProductAfterOrder(id,(data)=>{
        var orderId = data.data.order_id;
        var orderNo = data.data.order_no;
        this.payOrder(orderId,orderNo);
    })
  },
   //下单
  payOrder:function(orderId,orderNo)
  {
    user.getReturnPreOrder(orderId,orderNo,(data)=>{
         var out_trade_no = data.data.alipay_trade_create_response.out_trade_no;
         var trade_no = data.data.alipay_trade_create_response.trade_no;
         my.tradePay({
           tradeNO:trade_no,
           success: (res) => {
             if(res.resultCode ==9000)
                {
                  user.returnNotify(orderId,(data)=>{
                    if(data.code ==1 && data.data ==true)
                    {
                     my.showToast({
                       type:'success',
                       content:"买断成功",
                       success: (res) => {
                        this._loadData(this.data.options)
                       },
                     });
                    }
                  })
                }else{
                   my.showToast({
                       type:'fail',
                       content:"买断失败请重试",
                       success: (res) => {
                         this._loadData(this.data.options);
                       },
                     });
                }
           },
         });
    })
  },
  rentreturn(event)
  {
    my.makePhoneCall({
      number: '400-011-9619', // 电话号码
    });
  
  }
});
